package org.iks.store.business.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.iks.common.AjaxResult;
import org.iks.common.pojo.user.UserInfoVo;
import org.iks.common.util.JwtUtil;
import org.iks.common.util.ServletUtil;
import org.iks.store.business.service.SessionService;
import org.iks.store.business.vo.session.SessionMessageVo;
import org.iks.store.business.vo.session.SessionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Author kzou
 * Date 2024/2/13
 * Description:
 */
@Tag(name = "会话模块")
@RestController
@RequestMapping("session")
public class SessionController {
    @Autowired
    private SessionService sessionService;

//    @PostMapping("create")
//    public AjaxResult<?> createSession(@RequestBody List<String> members) {
//        String userAccount = ServletUtil.currUser().account();
//        int session = sessionService.createSession(userAccount, members);
//        return AjaxResult.success(session);
//    }

    @Operation(summary = "邀请好友")
    @PostMapping("/member/add")
    public AjaxResult<?> addUser(Integer sessionId, String account) {
        sessionService.addMember(sessionId, account);
        return AjaxResult.success();
    }

    @Operation(summary = "会话成员")
    @PostMapping("/member/query")
    public AjaxResult<List<UserInfoVo>> queryMembers(Integer sessionId) {
        List<UserInfoVo> result = sessionService.queryMembers(sessionId);
        return AjaxResult.success(result);
    }

    @Operation(summary = "退出会话")
    @PostMapping("/quit")
    public AjaxResult<?> quitSession(Integer sessionId) {
        String token = ServletUtil.getToken();
        String userAccount = JwtUtil.getUserAccount(token);
        sessionService.deleteMember(sessionId, userAccount);
        return AjaxResult.success();
    }

    @Operation(summary = "会话列表")
    @PostMapping("/list")
    public AjaxResult<List<SessionMessageVo>> querySessionList() {
        String token = ServletUtil.getToken();
        String userAccount = JwtUtil.getUserAccount(token);
        List<SessionMessageVo> result = sessionService.querySessionMessageList(userAccount);
        return AjaxResult.success(result);
    }

    @Operation(summary = "会话信息")
    @PostMapping("/query")
    public AjaxResult<SessionVo> querySession(Integer sessionId) {
        SessionVo result = sessionService.querySession(sessionId);
        return AjaxResult.success(result);
    }

    @Operation(summary = "未读消息个数")
    @PostMapping("/query/unReadMessage/count")
    public AjaxResult<Integer> queryUnReadMessageCount(Integer sessionId) {
        String token = ServletUtil.getToken();
        String userAccount = JwtUtil.getUserAccount(token);
        int result = sessionService.queryUnReadMessageCount(sessionId,userAccount);
        return AjaxResult.success(result);
    }

    @Operation(summary = "创建默认会话")
    @PostMapping("/create/default")
    public AjaxResult<Integer> queryUserDefaultSession(@RequestParam("friendAccount") String userAccount) {
        Integer id = sessionService.createUserDefaultSession(userAccount);
        return AjaxResult.success(id);
    }

}
